// pages/blood/blood.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: ['空腹', '早餐前', '早餐后', '午餐前', '午餐后', '晚餐前', '晚餐后', '睡眠前'],
    curIndex: 0,
    openTime: false,
    openNumber: false,
    page: 1,
    page_size: 10,
    normaUrl: '',
    isBottom: false,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData()
  },
  return_index () {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  return_act() {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  getData() {
    let that = this
    app.request(app.apiDomain + 'index.php?c=activity&a=getGlucoseList', {
      page: that.data.page,
      page_size: that.data.page_size
    }, function (res) {
      if (res.data.status == 1 && res.data.data.length > 0) {
        if (res.data.data.length < 10) {
          that.setData({
            isBottom: true
          })
        } else {
          that.setData({
            isBottom: false
          })
        }
        let data = res.data.data
        let maxNum = 0
        let arr = []
        let num = 1
        for (let item of data) {
          arr.push(item.to_date)
        }
        for (let j = 1; j < 9; j++) {
          if (data[0]['number' + j] != 0) {
            maxNum = data[0]['number' + j]
            num = j
          }
        }
        let date = data[0].update_time.split(' ')[1].substring(0,2)
        if (parseInt(date) >= 12) {
          date = data[0].update_time.split(' ')[0] + ' 下午' + data[0].update_time.split(' ')[1].substring(0, 5)
        } else {
          date = data[0].update_time.split(' ')[0] + ' 上午' + data[0].update_time.split(' ')[1].substring(0,5)
        }
        if (num == '1' || num == '2' || num == '4' || num == '6') {
          if (Number(maxNum) >= 4.4 && Number(maxNum) <= 7.0 ) {
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/blood_normal.png'
          } else if (Number(maxNum) < 4.4){
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/pd.png'
          } else if (Number(maxNum) > 7.0) {
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/pg.png'
          } 
        } else if (num == '3' || num == '5' || num == '7') {
          if (Number(maxNum) >= 7.8 && Number(maxNum) <= 10.0) {
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/blood_normal.png'
          } else if (Number(maxNum) < 7.8) {
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/pd.png'
          } else if (Number(maxNum) > 10.0) {
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/pg.png'
          } 
        } else if (num == '8') {
          if (Number(maxNum) >= 5.0 && Number(maxNum) <= 8.3) {
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/blood_normal.png'
          } else if (Number(maxNum) < 5.0) {
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/pd.png'
          } else if (Number(maxNum) > 8.3) {
            that.data.normaUrl = '//resource.160nurse.com/canvas/healthy/img/blood/pg.png'
          } 
        }
        let isNormal = 0
        if (that.data.normaUrl.indexOf('normal') !== -1) {
          isNormal = 0
        } else if (that.data.normaUrl.indexOf('pd.png') !== -1) {
          isNormal = 1
        } else if (that.data.normaUrl.indexOf('pg.png') !== -1) {
          isNormal = 2
        }
        that.setData({
          list: that.data.list.concat(data),
          titleArr: arr,
          maxNum: maxNum,
          date: date,
          normaUrl: that.data.normaUrl,
          isNormal: isNormal
        })
      }
    }, 'POST')
  },
  openTime () {
    this.setData({
      openTime: true
    })
  },
  bindChange (e) {
    let index = e.detail.value[0]
    this.curIndex = index
    console.log(index)
  },
  clickOk () {
    let that = this
    that.setData({
      curIndex: this.curIndex || 0,
      openTime: false
    })
    let type = 0
    if (parseInt(that.data.curIndex) == 0 || parseInt(that.data.curIndex) == 1 || parseInt(that.data.curIndex) == 3 || parseInt(that.data.curIndex) == 5 || parseInt(that.data.curIndex) == 7) {
      type = 5
    } else {
      type = 7
    }
    that.setData({
      page: 1
    })
    wx.navigateTo({
      url: 'selectNumber?number=' + that.data.curIndex + '&type=' + type,
    })
  },
  clickCancle() {
    this.setData({
      curIndex: 0,
      openTime: false
    })
  },
  getScroll () {
    console.log('到达底部')
    let that = this
    if (that.data.isBottom) {
      return false
    }
    that.data.isBottom = true
    that.data.page++
    that.getData()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    app.SystemInfo(this)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let nickName = '';
    let userId = 0;
    try {
      nickName = wx.getStorageSync('nickname');
      userId = wx.getStorageSync('user_id');
    } catch (e) { }

    return {
      path: "/pages/index/index?user_id=" + userId,
      title: '健康160血糖管理小助手，帮您更好地记录控制血糖！',
      imageUrl: app.resourceDomain + 'canvas/healthy/img/blood/share.png'
    }
  }
})